package com.d3works.apps.project.db.repo;

import com.d3works.apps.project.db.TaskTemplate;
import com.d3works.apps.project.exception.ProjectExceptionMessage;
import com.d3works.apps.project.service.TaskTemplateService;
import com.d3works.i18n.I18n;
import com.d3works.inject.Beans;
import javax.persistence.PersistenceException;

public class TaskTemplateManagementRepository extends TaskTemplateRepository {

  @Override
  public TaskTemplate save(TaskTemplate taskTemplate) {

    if (taskTemplate.getVersion() != 0
        && Beans.get(TaskTemplateService.class)
            .isParentTaskTemplateCreatedLoop(taskTemplate, taskTemplate.getParentTaskTemplate())) {
      throw new PersistenceException(
          I18n.get(ProjectExceptionMessage.TASK_TEMPLATE_PARENT_TASK_CREATED_LOOP));
    }

    return super.save(taskTemplate);
  }
}
